package com.frx.fakeroutexposed.fragments;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Spinner;

import com.frx.fakeroutexposed.R;
import com.frx.fakeroutexposed.tools.MY_NAMES;
import com.frx.fakeroutexposed.tools.SettingsHelper;
import com.github.clans.fab.FloatingActionButton;

import static android.content.Context.TELEPHONY_SERVICE;

/**调整模拟参数的 Fragment <br>
 * Created by viki on 2017/2/28.
 */

public class SettingsFragment extends Fragment {
    /**
     * 参数配置,
     */
    private SettingsHelper mSettings=null;
    /**
     * 新 IMEI 编辑框
     */
    private EditText mNewIMEIEditText =null;

    /**
     * 模拟 GPS 各个点的时间间隔
     * <br>数据选择框
     */
    private Spinner mFakeIntervalNumberPicker=null;
    /**
     * 目标程序包名的编辑框
     */
    private EditText mTargetPackageNameEditText =null;
    /**
     * 保存配置按钮
     */
    private FloatingActionButton fb_btn_save_settings = null;
    private static final String STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.settings_layout, container, false);
        mNewIMEIEditText =(EditText)view.findViewById(R.id.editText_new_imei);
        mTargetPackageNameEditText =(EditText)view.findViewById(R.id.editText_target_name);
        mFakeIntervalNumberPicker=(Spinner) view.findViewById(R.id.spinner_Pick_interval);
        fb_btn_save_settings=(FloatingActionButton)view.findViewById(R.id.fab_btn_save_settings);
        //初始化控件显示
        initTVDisplay();
        //保存按钮的监听事件
        fb_btn_save_settings.setOnClickListener(btnClickListener);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSettings = new SettingsHelper(this.getActivity());
    }
    //保存配置按钮的监听事件
    private View.OnClickListener btnClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //保存新的IMEI
            if(!("".equals(mNewIMEIEditText.getText().toString()))){
                // 编辑框内容不为空,保存
                mSettings.setString(MY_NAMES.KEY_IMEI, mNewIMEIEditText.getText().toString());
            }

            //保存目标程序的包名
            if(!("".equals(mTargetPackageNameEditText.getText().toString()))){
                // 编辑框内容不为空,保存
                mSettings.setString(MY_NAMES.KEY_TARGET_PACKAGE_NAME, mTargetPackageNameEditText.getText().toString());
            }

            //保存模拟 GPS 各个点的时间间隔
            //保存的是 adapter 的索引值
            mSettings.setInt(MY_NAMES.KEY_FAKE_TIME_INTERVAL_INDEX,
                    mFakeIntervalNumberPicker.getSelectedItemPosition());
        }
    };
    /***
     *  初始化各个控件的显示
     */
    private void initTVDisplay(){
        // IMEI 编辑框
        mNewIMEIEditText.setHint(mSettings.getString(MY_NAMES.KEY_IMEI,getIMEI()));
        // 目标程序包名编辑框
        mTargetPackageNameEditText.setHint(mSettings.getString(MY_NAMES.KEY_TARGET_PACKAGE_NAME
                ,MY_NAMES.DATA_defalut_target_package_name));
        // 模拟 GPS 时间间隔的 adapter的索引值
        mFakeIntervalNumberPicker.setSelection(
                mSettings.getInt(MY_NAMES.KEY_FAKE_TIME_INTERVAL_INDEX,
                0));
    }
    /***
     *  获取手机IMEI
     */
    @SuppressLint("HardwareIds")
    private String getIMEI(){
        TelephonyManager tm = (TelephonyManager) this.getActivity().getSystemService(TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
    }
}
